package com.dynamicthreadpool.strategy;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * DiscardOldestPolicy拒绝策略实现
 * 丢弃尚未执行的最老的任务，然后重试执行当前任务
 */
public class DiscardOldestPolicy implements RejectStrategy {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        if (!executor.isShutdown()) {
            // 移除队列头部的任务（最老的任务）
            executor.getQueue().poll();
            // 尝试再次提交当前任务
            executor.execute(r);
        }
    }

    @Override
    public String getPolicyName() {
        return "discardoldest";
    }
}
